package com.jeecg.p3.system.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.jeewx.api.core.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeecg.p3.commonweixin.entity.MyJwWebJwid;
import com.jeecg.p3.system.service.MyJwWebJwidService;

@Service("weixinAccountTokenTask")
@Transactional
public class WeixinAccountTokenTask {
	  private static final Logger logger = Logger.getLogger(WeixinAccountTokenTask.class);
	  
	  @Autowired
	  private MyJwWebJwidService myJwWebJwidService;
	
 	/**
	 * 循环每秒执行，查询超时的公众账号，重置Token
 	 * @throws Exception 
	 */
	public void autoResetToken() {
		long start = System.currentTimeMillis();
		//提前半小时重置Token
		long currentTime = new Date().getTime() - 1000 * 3600 * 2 + 30*60*1000;
		List<MyJwWebJwid> list = myJwWebJwidService.queryResetTokenList(new Date(currentTime));
		logger.info("===================定时任务【重置超过2小时失效token】开始===================");
		for(MyJwWebJwid account : list){
			try {
				myJwWebJwidService.resetAccessToken(account.getId());
			} catch (Exception e) {
				logger.info("---------定时任务【重置超过2小时失效token】失败公众号------------------"+account.getName());
			}
			
		}
		long end = System.currentTimeMillis();
		long times = end - start;
		logger.info("====================定时任务【重置超过2小时失效token】结束，" + "====执行重置公众号数量："+ (list!=null?list.size():0) +" ========总耗时"+times+"毫秒==========");
	}

		
}
